package com.example.teesystem.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.teesystem.entity.Issue;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
* @author NieYaShi
* @description 针对表【t_issue】的数据库操作Mapper
* @createDate 2022-03-28 09:23:20
* @Entity com.example.teesystem.entity.Issue
*/
@Mapper
public interface IssueMapper extends BaseMapper<Issue> {

    @Select({
            "<script>"+
            "select * from t_issue where id in " +
            "<foreach item = 'item' index = 'index' collection = 'ids' open='(' separator=',' close=')'>" +
            "#{item}" +
            "</foreach>"+
            "</script>"
    })
    List<Issue> getIssuesByIds(@Param("ids") List<Integer> ids);

    @Delete("update t_issue set is_delete = 1 where id = #{id}")
    void deleteIssue(int id);

    @Select("select * from t_issue where id = #{id}")
    Issue getIssueMessageById(Integer id);

    @Select("select * from t_issue where id = #{id} and course_id = #{courseId}")
    Issue getIssueByIdAndCourseId(Integer id, Integer courseId);

    @Select("select * from t_issue " +
            "where course_id = #{courseId} " +
            "and training_exam = 1 " +
            "and chapter_id = #{chapterId} " +
            "and type = #{type} " +
            "order by rand() " +
            "limit #{num}")
    List<Issue> getRandomIssue(int courseId,int chapterId,int type,int num);

    @Update("update t_issue set reference_num = reference_num+1 where id = #{id}")
    void addReferenceNum(int id);

    @Update("update t_issue set reference_num = reference_num-1 where id = #{id}")
    void subReferenceNum(int id);

    @Select("select * from t_issue where course_id = #{courseId} and training_exam = 0 and (type=0 || type=3 || type=s2) order by rand() limit #{num}")
    List<Issue> getPleasureTrainingIssue(int courseId,int chapterId,int type,int num);

    @Select("select * from t_issue where course_id = #{courseId} and is_delete = 0 and training_exam = 0 " +
            "and difficulty = IF(#{difficulty}=-1,difficulty,#{difficulty}) " +
            "and type = IF(#{type}=-1,type,#{type}) " +
            "and chapter_id = IF(#{chapterId}=-1,chapter_id,#{chapterId})" +
            "order by rand() limit 10")
    List<Issue> getInterestIssue(int courseId, int difficulty, int type, int chapterId);

    @Select("select * from t_issue where course_id = #{courseId} and training_exam = #{trainingExam} and is_delete = 0")
    List<Issue> getIssuesByCourseIdAndTrainingExam(int courseId, int trainingExam);

    @Select("select id from t_issue where course_id = #{courseId} and chapter_id = #{chapterId} and is_delete = 0")
    List<Integer> getIssueIdListByCourseIdAndChapterId(int courseId, int chapterId);

    @Select("select * from t_issue where course_id = #{courseId} and is_delete = 0")
    List<Issue> getIssueByCourseId(int courseId);
    
    int updateChapterIdByChapterId(@Param("chapterId") Integer chapterId, @Param("oldChapterId") Integer oldChapterId);
    
    List<Integer> getIdByCourseIdAndChapterIdAndTrainingExam(@Param("courseId") Integer courseId, @Param("chapterId") Integer chapterId, @Param("trainingExam") Integer trainingExam);
    
    List<Issue> getPkIssues(Integer courseId, Integer issueSize);

    @Select("select * from t_issue where course_id = #{courseId} and is_delete = 0 and training_exam = 0 " +
            "and (type = 0 || type = 2 || type = 3) " +
            "order by rand() limit 49")
    List<Issue> getBoatGameIssue(int courseId);

    @Select("select id from t_issue")
    List<Integer> get();
    @Select("update t_issue set issue_type = #{issueType} where id = #{id}")
    void update(String issueType,Integer id);

    @Select("select * from t_issue where course_id = #{courseId} and is_delete = 0 and training_exam = 0 " +
            "and (type = 0 || type = 2 || type = 3) " +
            "order by rand() limit 1")
    Integer getIssueByRandomOne(int courseId);
    @Update("update t_issue set difficulty = #{difficulty} where id = #{id}")
    void updateDifficulty(int difficulty,int id);

    @Select("select  * from t_issue")
    List<Issue> getall();

    @Select("select * from t_issue where course_id = #{courseId} and training_exam = 0 and is_delete = 0 and type = #{type} order by rand() limit 5")
    List<Issue> getSecondGameIssue(int courseId,int type);
}
